﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThouShaltNotBrick.Logic.Resources;
using ThouShaltNotBrick.Models;

namespace ThouShaltNotBrick.Logic.GeneralObjects
{
    public sealed class BrickStatesAndTypes
    {
        #region Properties
        private List<BrickState> mBrickStates;
        private List<BrickType> mBrickTypes;
        #endregion

        #region Constructors
        public BrickStatesAndTypes(
            GameResources pGameResources,
            GraphicsResources pGraphicsResources)
        {
            mBrickStates = ConstructBrickStates(pGameResources, pGraphicsResources);
            mBrickTypes = ConstructBrickTypes(pGameResources, mBrickStates);
        }
        #endregion

        #region Methods
        private static List<BrickState> ConstructBrickStates(
            GameResources pGameResources,
            GraphicsResources pGraphicsResources)
        {
            List<BrickStateData> brickStateDataList = pGameResources.BrickStatesData.GetBrickStates();
            List<BrickState> brickStates = brickStateDataList
                .Select(
                    bsd => new BrickState(
                        bsd.Name,
                        bsd.IsIndestructible,
                        pGraphicsResources.GetBrickStateTexture(bsd.TextureFileName)))
                .ToList();

            return brickStates;
        }

        private static List<BrickType> ConstructBrickTypes(
            GameResources pGameResources,
            List<BrickState> pBrickStates)
        {
            List<BrickTypeData> brickTypeDataList = pGameResources.BrickTypesData.GetBrickTypes();
            List<BrickType> brickTypes = brickTypeDataList
                .Select(
                    btd => new BrickType(
                        btd.Name,
                        btd.GetBrickStates()
                            .Select(
                                bsName => pBrickStates.First(bs => bs.Name == bsName))
                            .ToList()))
                .ToList();

            return brickTypes;
        }

        public BrickType GetBrickType(string pBrickTypeName)
        {
            return mBrickTypes.First(bt => bt.Name == pBrickTypeName);
        }
        #endregion
    }
}